package com.scs.application.modules.upms.vo;

import com.alibaba.fastjson.annotation.JSONField;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.utils.StringUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @Description:ResultVerifyLoginVO
 */
@Data
@EqualsAndHashCode(callSuper = false)
@ToString(callSuper = true)
@Accessors(chain = true)
public class ResultVerifyLoginVO implements Serializable {


    /**
     * 验证状态
     */
    private Status status;

    /**
     * 返回的验证信息（用户可见）
     *
     */
    private String msg;

    /**
     * token
     *  当前 status = ok 时token有值
     */
    private String jti;

    /* 基本参数*/
    /**
     * 用户名
     */
    private String username;

    /**
     * 手机号码
     */
    private String mobile;


    /* 状态参数*/
    // short 使用  GlobalConsts.NO GlobalConsts.YSE
    /**
     * 需要修改（登录密码）
     * 两种情况： 密码过期、初始密码登录
     *
     */
    private short needModify;

    /**
     * 签名
     *      携带此签名方可通modify.html页面修改密码
     */
    private String Sign;

    /**
     * 需要手机号 (即 未绑定手机号码时)
     */
//    private short needMobile;

    /**
     * 显示手机号
     */
//    private short showMobile;

    /**
     * 双因素登录
     *
     */
//    private short doubleFactor;

    /**
     * 验证状态
     */
    public enum Status{

        OK(200,"验证通过"),
        PASSWORD_EXPIRED(201,"密码过期,请尽快更新"),
        DOUBLE_FACTOR_LOGIN(202,"请继续完成双因素登录"),
        WRONG_PWD(400,"用户名或密码错误"),
        WRONG_CODE(401,"手机验证码不正确"),
        WRONG_PHONE(402,"您的账号还没有绑定手机号码"),
        MORE_PHONE(409,"手机号码绑定了多个账号，请使用用户名登录"),
        FORBIDDEN(403, "操作过于频繁，请稍后再试"),
        NONE_FOUND(404,"用户名或密码错误.")
        ;

        private final int value;
        private final String reasonPhrase;


        private Status(int value, String reasonPhrase) {
            this.value = value;
            this.reasonPhrase = reasonPhrase;
        }

        public int value() {
            return this.value;
        }

        public String getReasonPhrase() {
            return this.reasonPhrase;
        }

        private int valueOf(){
            return this.value / 100;
        }

        public Boolean isSuccess(){
            return valueOf() == series.SUSSECC.value;
        }

        public Boolean isError(){
            return valueOf() == series.ERROR.value;
        }

        public enum  series{
            SUSSECC(2),
            ERROR(4);

            private final int value;
            private series(int value){
                this.value = value;
            }

        }
    }


    /**
     * 通用返回实体类
     * @return
     */
    @JSONField(serialize = false, deserialize = false)
    public CommonResult convertToCommonResult() {
        if (StringUtils.isBlank(this.getMsg())) {
            this.setMsg(this.getStatus().getReasonPhrase());
        }

        if (this.status.isSuccess()) {
            return CommonResult.ok(this.msg, this);
        } else {
            return CommonResult.error(this.msg, this);
        }

    }
}
